package com.tbit.main.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.OperaPswService;
import com.tbit.manager.api.RideCardApi;
import com.tbit.manager.api.dto.PackageDelayDTO;
import com.tbit.manager.api.dto.RideCardDTO;
import com.tbit.manager.api.dto.UserMemberDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 骑行卡
 */
@Validated
@RestController
@RequestMapping(value = "/rideCard")
public class RideCardController {

    @Autowired
    private RideCardApi rideCardApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 用户购买的骑行套餐记录分页
     *
     * @param userId     用户id
     * @param pageSize   条数
     * @param pageNumber 页码
     * @return Result<List < UserMemberDTO>>
     */
    @GetMapping("/userRideCardPage")
    public Result<List<UserMemberDTO>> userRideCardPage(@RequestParam("userId") @NotNull Integer userId,
                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                        @RequestParam(defaultValue = "1") Integer pageNumber) {
        List<UserMemberDTO> userMemberDTOList = rideCardApi.userRideCardPage(userId, pageSize, pageNumber);
        return Result.success(userMemberDTOList);
    }

    /**
     * 延长骑行卡时间
     *
     * @param delayDTO .
     * @return Result<List < UserMemberDTO>>
     */
    @GetMapping("/modifyExpireTime")
    public Result<String> modifyExpireTime(@Validated PackageDelayDTO delayDTO) {
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(delayDTO.getPwd())) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(),
                    delayDTO.getPwd());
        }
        com.tbit.common.entity.view.Result<String> result = rideCardApi.modifyExpireTime(delayDTO);
        Result<String> mainResult = Result.success();
        if (result.unSuccess()) {
            mainResult = Result.error(result.getMsg());
        }
        return mainResult;
    }

    /**
     * 修改用户骑行卡次数
     *
     * @param rideCardDTO .
     */
    @RequestMapping("/userRideCardEdit")
    public Result<String> userRideCardEdit(@Validated RideCardDTO rideCardDTO) {
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(rideCardDTO.getOperaPsw())) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(),
                    rideCardDTO.getOperaPsw());
        }
        com.tbit.common.entity.view.Result<String> result = rideCardApi.userRideCardEdit(rideCardDTO);
        Result<String> mainResult = Result.success();
        if (result.unSuccess()) {
            mainResult = Result.error(result.getMsg());
        }
        return mainResult;
    }
}
